/*
 * @Description: Joker desc
 * @Author: Joker
 * @Date: 2020-09-10 11:34:11
 * @LastEditTime: 2020-09-22 16:34:36
 * @LastEditors: Joker
 */
import axios from 'axios'
import { Notification } from 'element-ui'
// import store from '../store'
import { _getToken } from '@/util/token'
import _buildFormParams from '@/util/form'
import router from '../router'
const qs = require('qs')

// 创建axios实例
const _bwAxios = axios.create({
    baseURL: 'http://localhost:9090',
    timeout: 30000, // 请求超时时间
    paramsSerializer: params => qs.stringify(params, {
        arrayFormat: 'repeat'
    })
})

// 请求拦截器
_bwAxios.interceptors.request.use(config => {
    const isPutOrPost = ['put', 'post'].includes(config.method)
    const isJson = config.headers['Content-Type'] === 'application/json'
    if (isPutOrPost && isJson) {
        config.data = JSON.stringify(config.data)
    }
    if (_getToken()) {
        config.headers.Authorization = _getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    if (isPutOrPost && !isJson) {
        if (config.setConfig && config.setConfig.formData) {
            config.data = _buildFormParams(config.data)
        } else {
            config.data = qs.stringify(config.data, { arrayFormat: 'repeat' })
        }
    }
    return config
}, error => {
    Promise.reject(error)
})

// 响应拦截器
_bwAxios.interceptors.response.use(
    response => {
        console.log('进入axios:response...')
        const _data = response.data
        const _status = response.status
        if (_status !== 200 && _data.code !== 200) {
            Notification({
                message: _data.message || '通用错误',
                type: 'error',
                duration: 3 * 1000
            })
        }
        return _data
    }, error => {
        console.log('进入axios:error...')
        if (error === undefined || error.code === 'ECONNABORTED') {
            Notification({
                message: '服务请求超时',
                type: 'warning',
                duration: 3 * 1000
            })
            return Promise.reject(error)
        }
        const { response: { status, data: { message = '服务器发生错误' } } } = error
        const { response } = error
        const tipMsg = response.data.message || message

        if (status === 400) {
            Notification({
                message: tipMsg,
                type: 'error'
            })
        } else if (status === 401) {
            // store.dispatch('logout', '登录凭证失效')
            Notification({
                message: tipMsg || '未登录',
                type: 'error',
                onClose: () => {
                    router.replace('/login')
                }
            })
        } else if (status === 403) {
            Notification({
                message: tipMsg || '权限不足',
                type: 'warning'
            })
        } else {
            Notification({
                message: 'code : ' + response.data.code + ' , message : ' + tipMsg,
                type: 'error'
            })
        }
        return Promise.reject(error)
    }
)

export const Method = {
    GET: 'get',
    POST: 'post',
    PUT: 'put',
    DELETE: 'delete'
}
export default _bwAxios
